﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AMing.Core.Extension;

namespace AMing.Control
{
    /// <summary>
    /// Slider_TimeLine.xaml 的交互逻辑
    /// </summary>
    public partial class Slider_TimeLine : UserControl
    {
        public Slider_TimeLine()
        {
            InitializeComponent();
        }

        #region Attribute

        private double _max = 0;
        /// <summary>
        /// 最大值
        /// </summary>
        public double Max
        {
            get { return _max; }
            set
            {
                _max = value;
                this.ResetProgress();
            }
        }

        private double _val = 0;
        /// <summary>
        /// 当前的值
        /// </summary>
        public double Value
        {
            get { return _val; }
            set
            {
                _val = value;
                this.ResetProgress();
            }
        }

        /// <summary>
        /// Value 的百分比
        /// </summary>
        public double Percent
        {
            get
            {
                return Value / Max;
            }
        }

        private Func<double, string> _converter = (val) => { return val.ToString(); };
        /// <summary>
        /// 显示的类型
        /// </summary>
        public Func<double, string> Converter
        {
            get { return _converter; }
            set
            {
                _converter = value;
            }
        }


        private Func<double, string> _showValueConverter = (val) => { return val.ToString(); };
        /// <summary>
        /// 显示数据的转换器
        /// </summary>
        public Func<double, string> ShowValueConverter
        {
            get { return _showValueConverter; }
            set
            {
                _showValueConverter = value;
            }
        }

        #endregion

        #region Method
        /// <summary>
        /// 重新设置进度条长度
        /// </summary>
        private void ResetProgress()
        {
            double max = grid_timeline.ActualWidth;
            double w = this.Percent * max;
            re_timeline.Width = w;
        }
        /// <summary>
        /// 触发改变值的事件
        /// </summary>
        private void TriggerChangeValue()
        {
            Point p = Mouse.GetPosition(grid_timeline);
            double max = grid_timeline.ActualWidth;
            double val = p.X;
            double per = val / max;
            double newval = per * this.Max;
            if (this.ChangeValue != null)
            {
                this.ChangeValue(newval);
            }
        }

        /// <summary>
        /// 显示  显示位置时间 
        /// </summary>
        private void ShowTimeText()
        {
            //timer.Start();
            grid_timetext.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 隐藏  显示位置时间 
        /// </summary>
        private void HideTimeText()
        {
            //timer.Stop();
            grid_timetext.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 改变  显示位置时间 
        /// </summary>
        private void TimeTextChange()
        {
            Point p = Mouse.GetPosition(grid_timeline);
            double max = grid_timeline.ActualWidth;
            double val = p.X;
            double per = val / max;
            double newval = per * this.Max;
            if (ShowValueConverter != null)
            {
                txt_time.Text = ShowValueConverter(newval);
            }
            else
            {
                txt_time.Text = newval.ToString();
            }
        }
        /// <summary>
        /// 移动  显示位置时间 
        /// </summary>
        private void TimeTextMove()
        {
            Point p = Mouse.GetPosition(grid_timeline);
            double val = p.X;
            grid_timetext.Margin = new Thickness(val, 0, 0, 0);
        }
        #endregion

        #region Event
        /// <summary>
        /// 改变值
        /// </summary>
        public event Action<double> ChangeValue;

        private void grid_timeline_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            this.TriggerChangeValue();
        }

        private void grid_timeline_SizeChanged_1(object sender, SizeChangedEventArgs e)
        {
            this.ResetProgress();
        }

        private void grid_timeline_MouseEnter_1(object sender, MouseEventArgs e)
        {
            ShowTimeText();
        }

        private void grid_timeline_MouseLeave_1(object sender, MouseEventArgs e)
        {
            HideTimeText();
        }

        private void grid_timeline_MouseMove_1(object sender, MouseEventArgs e)
        {
            TimeTextMove();
            TimeTextChange();
        }

        #endregion

        public enum Slider_ShowType
        {
            TimeLine,
            Value,

        }
    }
}
//timeline.ShowValueConverter = (val) =>
//{
//    TimeSpan timeSpan = TimeSpan.FromSeconds(val);
//    return timeSpan.ToString_HHmmss();
//};
//vol.ShowValueConverter = (val) =>
//{
//    return string.Format("{0:D}", val * 100);
//};